﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace BigData
{
    class BigDataContainer : IDisposable
    {
        public string Path { get; private set; }
        public Dictionary<string, StreamWriter> Dic { get; private set; } = new Dictionary<string, StreamWriter>();
        public BigDataContainer(string path)
        {
            this.Path = path;
        }

        /// <summary>
        /// 线程安全
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="line"></param>
        public void WriteLine(string filename, string line)
        {
            filename = filename.ToLower();
            if (!Dic.ContainsKey(filename))
            {
                lock (Dic)
                {
                    if (!Dic.ContainsKey(filename))
                    {
                        Dic.Add(filename, new StreamWriter($"{Path}\\{filename}.txt"));
                    }
                }
            }

            lock (Dic[filename])
            {
                Dic[filename].WriteLine(line);
            }
        }
        public void Dispose()
        {
            foreach (var sw in Dic.Values)
            {
                sw.Close();
                sw.Dispose();
            }
        }

    }
}
